package com.fzfnn.scrapbook.controller;

import com.fzfnn.scrapbook.entity.CollectionFolder;
import com.fzfnn.scrapbook.service.CollectionService;
import com.fzfnn.scrapbook.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/journal/collection")
@Tag(name = "收藏接口", description = "收藏手账相关接口")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    /**
     * 创建收藏夹
     */
    @PostMapping("/folder")
    @Operation(summary = "创建收藏夹")
    public R createFolder( @RequestParam String name, @RequestHeader("Authorization") String token) {
        return collectionService.createFolder(token, name);
    }

    /**
     * 获取用户收藏夹列表
     */
    @GetMapping("/folders")
    @Operation(summary = "获取用户收藏夹列表,有userid用userid，没有用token")
    public R getUserFolders(@RequestHeader("Authorization") String token,@RequestParam(required = false) Integer userid) {
        return collectionService.getUserFolders(token,  userid);
    }

    /**
     * 添加手帐到收藏夹
     */
    @PostMapping("/add")
    @Operation(summary = "添加手帐到收藏夹")
    public R addToCollection(@RequestParam Long journalId,
                             @RequestParam Long folderId, @RequestHeader("Authorization") String token) {
        return collectionService.addToCollection(token, journalId, folderId);
    }

    /**
     * 判断是否已收藏
     */
    @GetMapping("/is-collected")
    @Operation(summary = "判断是否已收藏")
    public R isCollected( @RequestParam Long journalId, @RequestHeader("Authorization") String token) {
        return  collectionService.isCollected(token, journalId);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/remove")
    @Operation(summary = "取消收藏")
    public R removeFromCollection( @RequestParam Long journalId, @RequestHeader("Authorization") String token) {
        return collectionService.removeFromCollection(token, journalId);
    }
    // 删除收藏夹（新增接口）
    @DeleteMapping("/folder")
    @Operation(summary = "删除收藏夹，会删除收藏夹下的所有收藏")
    public R deleteFolder(@RequestParam Long folderId, @RequestHeader("Authorization") String token) {
        return collectionService.deleteFolder(folderId,token);
    }
    @GetMapping("/folder-collections")
    @Operation(summary = "获取收藏夹下的所有收藏")
    public R getFolderCollections(@RequestHeader("Authorization") String token,
                                  @RequestParam Long folderId) {
        return collectionService.getCollectionsByFolder(token, folderId);
    }

    @PutMapping("/name")
    @Operation(summary = "修改收藏夹名称")
    public R updateFolderName(@RequestParam Long folderId,
                              @RequestParam String name, @RequestHeader("Authorization") String token) {
        return collectionService.updateFolderName(token, folderId, name);
    }

}
